(function () {
    var exports = this;

    var $ = null;

    var tilizhiConfig = {
        'HPMinValue_0': {val: 200, hint: '玩家体力值低于此门槛时无法进行游戏', min: -999999, max: 999999, everyadd: 1, everyreduce: 1},
        'HPMinCsp_0': {val: 0, hint: '玩家单场结算输赢分数小于\n此门槛时不计算消耗体力值', min: 0, max: 999999, everyadd: 1, everyreduce: 1},
        'HPAverage_0': {val: 0, hint: '设置玩家每场次体力值消耗', min: 0, max: 999999, everyadd: 1, everyreduce: 1},
        'HPTpye2_0': {val: 1, hint: '当出现多个大赢家时按照\n总分数平均消耗体力值'},
        'HPTpye2_1': {val: 2, hint: '当出现多个大赢家时每个大赢家\n均按照分数消耗体设定体力值'},
        'HPTpye1_0': {val: 1, hint: '按照设定值消耗玩家体力值'},
        'HPTpye1_1': {val: 2, hint: '按照玩家分数按比例消耗体力值'},
        'HPValue_0': {val: 0, min: 0, max: 999999, everyadd: 1, everyreduce: 1},
        'HPValue_1': {val: 0, min: 0, max: 999999, everyadd: 1, everyreduce: 1, ispercent: true},
        'HPMaxC_0': {val: 0, min: 0, max: 999999, everyadd: 1, everyreduce: 1},

        'HPValuedang1_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedangcost1_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedang2_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedangcost2_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedang3_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedangcost3_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedang4_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedangcost4_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedang5_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true},
        'HPValuedangcost5_0': {val: 0, min: 0, max: 99999, everyadd: 1, everyreduce: 1, islevel: true}
    };
    var ClubPowerCreateRoomLayer = cc.Layer.extend({
        ctor: function (parentLayer) {
            this._super();

            var scene = ccs.load(club.res.ClubPowerCreateLayer_json);
            this.addChild(scene.node);
            $ = create$(this.getChildByName('Layer'));


            this._parentLayer = parentLayer;
            this.initUI();
            return true;
        },
        initUI: function () {
            var that = this;
            for (var _k in tilizhiConfig) {
                var _curC = tilizhiConfig[_k];
                (function (k, curC) {
                    var node = $('tiliNode.' + k);
                    var txt = node.getChildByName('txt');
                    var btn_jia = node.getChildByName('btn_jia');
                    var btn_jian = node.getChildByName('btn_jian');
                    var bg = node.getChildByName('bg');
                    var hint = node.getChildByName('hint');
                    if (txt) {
                        if (curC['max']) txt.setString(curC['val']);
                        node.val = curC['val'];
                    }
                    if (hint) {
                        hint.setVisible(curC['hint']);
                        if (curC['hint']) {
                            var hintListener = cc.EventListener.create({
                                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                                swallowTouches: true,
                                onTouchBegan: function (touch, event) {
                                    if (!TouchUtil.isTouchMe(hint, touch, event)) {
                                        return false;
                                    }
                                    if (that._parentLayer.getChildByName('PopTishiLayer')) {
                                        return false;
                                    }
                                    var PopTishiLayer = loadNodeCCSItem(hall.res.PopTiShiUnit_json, that._parentLayer);
                                    if (!PopTishiLayer) {
                                        return false;
                                    }
                                    PopTishiLayer.setName('PopTishiLayer');
                                    var panel = PopTishiLayer.getChildByName('root').getChildByName('panel');
                                    var lb_content = panel.getChildByName('lb_content');
                                    lb_content.setVisible(false);
                                    var cloneContent = duplicateNode(lb_content);
                                    cloneContent.setVisible(true);
                                    cloneContent.boundingWidth = 500;
                                    cloneContent.setString(curC['hint']);
                                    panel.setContentSize(cc.size(cloneContent.getContentSize().width + 20, cloneContent.getContentSize().height + 20));
                                    cloneContent.setPosition(cc.p(panel.getContentSize().width / 2, panel.getContentSize().height / 2));
                                    panel.addChild(cloneContent);

                                    var pos = hint.getPosition();
                                    var WorldPos = hint.getParent().convertToWorldSpace(pos);
                                    WorldPos.x = WorldPos.x - panel.getContentSize().width < 0 ? panel.getContentSize().width : WorldPos.x;
                                    WorldPos.y = WorldPos.y + panel.getContentSize().height > cc.director.getVisibleSize().height ? cc.director.getVisibleSize().height - panel.getContentSize().height : WorldPos.y;
                                    WorldPos.x += cc.winSize.width / 2;
                                    WorldPos.y += cc.winSize.height / 2;
                                    panel.setPosition(WorldPos);
                                    return true;
                                },
                                onTouchMoved: function (touch, event) {
                                },
                                onTouchEnded: function () {
                                    if (that._parentLayer.getChildByName('PopTishiLayer')) {
                                        that._parentLayer.getChildByName('PopTishiLayer').removeFromParent();
                                    }
                                },
                                onTouchCancelled: function () {
                                    if (that._parentLayer.getChildByName('PopTishiLayer')) {
                                        that._parentLayer.getChildByName('PopTishiLayer').removeFromParent();
                                    }
                                }
                            });
                            cc.eventManager.addListener(hintListener, hint);
                        }
                    }
                    TouchUtil.setOnclickListener(bg, function () {
                        var callback = function (t) {
                            t = parseInt(t);
                            if (curC['max']) {
                                if (t < curC['min'] || t > curC['max']) {
                                    showAlert('输入的数字不在合理的区间内');
                                    return;
                                }
                                node.val = t;
                                txt.setString(t + (curC['ispercent'] ? '%' : ''));
                            }
                        };
                        window.curScene.addChild(new ClubInputLayer('setPower', null, callback), ZORDER.WINDOW);
                    }, {effect: TouchUtil.effects.NONE});
                    TouchUtil.setContinuousClickListener(btn_jia, function () {
                        var v = node.val;
                        v = parseInt(v);
                        if (curC['max']) {
                            if (v < curC['max']) v = v + curC['everyadd'];
                            txt.setString(curC['ispercent'] ? (v + '%') : v);
                        } else {
                            v++;
                            txt.setString(v);
                        }
                        node.val = v;
                    }, {});
                    TouchUtil.setContinuousClickListener(btn_jian, function () {
                        node.val = parseInt(node.val);
                        var v = node.val;
                        v = parseInt(v);
                        if (curC['max']) {
                            if (v > curC['min']) v = v - curC['everyreduce'];
                            txt.setString(curC['ispercent'] ? (v + '%') : v);
                        } else {
                            v--;
                            txt.setString(v);
                        }
                        node.val = v;
                    }, {});
                })(_k, _curC);

            }
            TouchUtil.setOnclickListener($('btn_open'), this.clickPowerOpen.bind(this));
            TouchUtil.setOnclickListener($('tiliNode.btn_dwsz'), this.clickDwsz.bind(this));
            TouchUtil.setOnclickListener($('tiliNode.btn_zfsyj'), this.clickZfsyj.bind(this));
            //固定值
            TouchUtil.setOnclickListener($('tiliNode.HPTpye1_0.touch'), function () {
                $('tiliNode.HPTpye1_1').getChildByName('checkBox').setSelected(false);
                $('tiliNode.HPTpye1_0').getChildByName('checkBox').setSelected(true);

                that.setGuDingZhi(true);
            });
            // 百分比
            TouchUtil.setOnclickListener($('tiliNode.HPTpye1_1.touch'), function () {
                $('tiliNode.HPTpye1_0').getChildByName('checkBox').setSelected(false);
                $('tiliNode.HPTpye1_1').getChildByName('checkBox').setSelected(true);

                that.setGuDingZhi(false);
            });
            // 平摊
            TouchUtil.setOnclickListener($('tiliNode.HPTpye2_0.touch'), function () {
                $('tiliNode.HPTpye2_1').getChildByName('checkBox').setSelected(false);
                $('tiliNode.HPTpye2_0').getChildByName('checkBox').setSelected(true);
            });
            //单算
            TouchUtil.setOnclickListener($('tiliNode.HPTpye2_1.touch'), function () {
                $('tiliNode.HPTpye2_0').getChildByName('checkBox').setSelected(false);
                $('tiliNode.HPTpye2_1').getChildByName('checkBox').setSelected(true);
            });

            $('tiliNode.HPTpye1_1').getChildByName('checkBox').setSelected(false);
            $('tiliNode.HPTpye1_0').getChildByName('checkBox').setSelected(true);
            $('tiliNode.HPTpye2_1').getChildByName('checkBox').setSelected(false);
            $('tiliNode.HPTpye2_0').getChildByName('checkBox').setSelected(true);
            this.clickPowerOpen();
            for (var i = 1; i <= 5; i++) {
                (function (row) {
                    var btnplus = $('dwNode' + row + '.plus');
                    var btnadd = $('dwNode' + row + '.add');
                    if (btnplus) {
                        btnplus.row = row;
                        TouchUtil.setOnclickListener(btnplus, function (btn) {
                            var currow = btn.row;
                            if (currow > 1) {
                                $('dwNode' + (currow)).setVisible(false);
                                $('tiliNode.HPValuedang' + (currow) + '_0').setVisible(false);
                                $('tiliNode.HPValuedangcost' + (currow) + '_0').setVisible(false);
                                if ($('dwNode' + (currow - 1) + '.plus')) $('dwNode' + (currow - 1) + '.plus').setVisible(true);
                                if ($('dwNode' + (currow - 1) + '.add')) $('dwNode' + (currow - 1) + '.add').setVisible(true);
                            }
                        });
                    }
                    if (btnadd) {
                        btnadd.row = row;
                        TouchUtil.setOnclickListener(btnadd, function (btn) {
                            var currow = btn.row;
                            if (currow < 5) {
                                if (currow == 1) {
                                    if ($('tiliNode.HPValuedang1_0').val < $('tiliNode.HPMinCsp_0').val) {
                                        showAlert('设置有误！初始档位值不能小于大赢家消耗门槛值，请修改！');
                                        return;
                                    }
                                } else {
                                    if ($('tiliNode.HPValuedang' + currow + '_0').val <= $('tiliNode.HPValuedang' + (currow - 1) + '_0').val) {
                                        showAlert('设置有误！高档位设置值不能低于低档位设置值，请修改！');
                                        return;
                                    }
                                }
                                $('dwNode' + (currow + 1)).setVisible(true);
                                $('tiliNode.HPValuedang' + (currow + 1) + '_0').setVisible(true);
                                $('tiliNode.HPValuedangcost' + (currow + 1) + '_0').setVisible(true);
                                if ($('dwNode' + currow + '.plus')) $('dwNode' + currow + '.plus').setVisible(false);
                                if ($('dwNode' + currow + '.add')) $('dwNode' + currow + '.add').setVisible(false);
                                if ($('dwNode' + (currow + 1) + '.plus')) $('dwNode' + (currow + 1) + '.plus').setVisible(true);
                                if ($('dwNode' + (currow + 1) + '.add')) $('dwNode' + (currow + 1) + '.add').setVisible(true);
                            }
                        });
                    }
                })(i);
            }
        },
        /*
         固定值  百分比
         */
        setGuDingZhi: function (flag) {
            if (flag) {
                $('tiliNode.HPMaxC_0').setVisible(false);
                $('tiliNode.HPValue_0').setVisible(true);
                $('tiliNode.HPValue_1').setVisible(false);
                $('tiliNode.word_dyjfd').setVisible(false);
                //显示档位设置
                $('tiliNode.word_dwsz').setVisible(true);
                $('tiliNode.btn_dwsz').setVisible(true);
                var dwszvis = $('tiliNode.btn_dwsz.on').isVisible();
                for (var i = 1; i <= 5; i++) {
                    $('dwNode' + i).setVisible(i == 1 && dwszvis);
                    $('tiliNode.HPValuedang' + i + '_0').setVisible(i == 1 && dwszvis);
                    $('tiliNode.HPValuedangcost' + i + '_0').setVisible(i == 1 && dwszvis);
                }
                $('dwNode1.add').setVisible(true);
            } else {
                $('tiliNode.HPMaxC_0').setVisible(true);
                $('tiliNode.HPValue_0').setVisible(false);
                $('tiliNode.HPValue_1').setVisible(true);
                $('tiliNode.word_dyjfd').setVisible(true);
                //显示档位设置
                $('tiliNode.word_dwsz').setVisible(false);
                $('tiliNode.btn_dwsz').setVisible(false);
                for (var i = 1; i <= 5; i++) {
                    $('dwNode' + i).setVisible(false);
                    $('tiliNode.HPValuedang' + i + '_0').setVisible(false);
                    $('tiliNode.HPValuedangcost' + i + '_0').setVisible(false);
                }
            }
        },
        /*
         档位设置
         */
        clickDwsz: function () {
            var vis = $('tiliNode.btn_dwsz.on').isVisible();
            $('tiliNode.btn_dwsz.on').setVisible(!vis);
            for (var i = 1; i <= 5; i++) {
                $('dwNode' + i).setVisible(i == 1 && vis == false);
                $('tiliNode.HPValuedang' + i + '_0').setVisible(i == 1 && vis == false);
                $('tiliNode.HPValuedangcost' + i + '_0').setVisible(i == 1 && vis == false);
            }
            $('dwNode1.add').setVisible(true);
            $('tiliNode.HPValue_0').setVisible(vis);
        },
        /*
         正分算赢家
         */
        clickZfsyj: function () {
            var vis = $('tiliNode.btn_zfsyj.on').isVisible();
            if (vis) {
                $('tiliNode.btn_zfsyj.on').setVisible(!vis);
                $('tiliNode.HPTpye2_0').setVisible(true);
            } else {
                $('tiliNode.btn_zfsyj.on').setVisible(!vis);
                $('tiliNode.HPTpye2_0').getChildByName('checkBox').setSelected(false);
                $('tiliNode.HPTpye2_0').setVisible(false);
                $('tiliNode.HPTpye2_1').getChildByName('checkBox').setSelected(true);
            }
        },
        clickPowerOpen: function () {
            var vis = $('btn_open.on').isVisible();
            $('btn_open.on').setVisible(!vis);
            var zfsyjvis = $('tiliNode.btn_zfsyj.on').isVisible();
            vis = !vis;

            $('tiliNode').setVisible(vis);
            $('tiliNode').setVisible(vis);

            $('tiliNode.HPMinValue_0').setVisible(vis);
            $('tiliNode.HPMinCsp_0').setVisible(vis);
            $('tiliNode.HPAverage_0').setVisible(vis);
            $('tiliNode.HPTpye1_0').setVisible(vis);
            $('tiliNode.HPTpye1_1').setVisible(vis);
            $('tiliNode.HPMaxC_0').setVisible(vis);
            $('tiliNode.HPTpye2_0').setVisible(vis);
            $('tiliNode.HPTpye2_1').setVisible(vis);
            $('tiliNode.HPTpye2_0').getChildByName('checkBox').setSelected(true);
            if (vis) {
                ////选了固定值
                if ($('tiliNode.HPTpye1_0') && $('tiliNode.HPTpye1_0').getChildByName('checkBox').isSelected()) {
                    $('tiliNode.HPValue_0').setVisible(true);
                    $('tiliNode.HPValue_1').setVisible(false);
                    $('tiliNode.HPMaxC_0').setVisible(false);
                    $('tiliNode.word_dyjfd').setVisible(false);
                } else {
                    $('tiliNode.HPValue_0').setVisible(false);
                    $('tiliNode.HPValue_1').setVisible(true);
                    $('tiliNode.HPMaxC_0').setVisible(true);
                    $('tiliNode.word_dyjfd').setVisible(true);
                }
                for (var i = 1; i <= 5; i++) {
                    $('dwNode' + i).setVisible(i == 1 && vis == false);
                    $('tiliNode.HPValuedang' + i + '_0').setVisible(i == 1 && vis == false);
                    $('tiliNode.HPValuedangcost' + i + '_0').setVisible(i == 1 && vis == false);
                }
                $('dwNode1.add').setVisible(true);
            } else {
                $('tiliNode.HPValue_0').setVisible(false);
                for (var i = 1; i <= 5; i++) {
                    $('dwNode' + i).setVisible(false);
                    $('tiliNode.HPValuedang' + i + '_0').setVisible(false);
                    $('tiliNode.HPValuedangcost' + i + '_0').setVisible(false);
                }
                $('tiliNode.btn_dwsz.on').setVisible(false);
            }
            if (zfsyjvis) {
                $('tiliNode.HPTpye2_0').getChildByName('checkBox').setSelected(false);
                $('tiliNode.HPTpye2_0').setVisible(false);
                $('tiliNode.HPTpye2_1').getChildByName('checkBox').setSelected(true);
            }
        },
        getWanfapara: function () {
            var para = {};
            var btn_zfsyjvis = $('tiliNode.btn_zfsyj.on').isVisible();
            var btn_dwszvis = $('tiliNode.btn_dwsz.on').isVisible();
            para['HPWinType'] = btn_zfsyjvis ? 1 : 0;
            para['HPCSLvState'] = btn_dwszvis ? 1 : 0;
            if (btn_dwszvis) {
                para['HPCSLvMap'] = [];
                for (var l = 5; l >= 1; l--) {
                    var HPValuedang = $('tiliNode.HPValuedang' + l + '_0');
                    var HPValuedangcost = $('tiliNode.HPValuedang' + l + '_0');
                    if(HPValuedang && HPValuedang.val > 0) {
                        para['HPCSLvMap'].push({score: HPValuedang.val, value: HPValuedangcost.val});
                    }
                }
            }

            for (var k in tilizhiConfig) {
                var conf = tilizhiConfig[k];
                var key = k.split('_')[0];
                var node = $('tiliNode.' + k);
                if (node && node.isVisible() && !conf['islevel']) {
                    var checkBox = node.getChildByName('checkBox');
                    if (checkBox && checkBox.isSelected()) {
                        para[key] = true;
                    } else {
                        para[key] = node.val;
                    }
                }
            }
            return para;
        },
    });

    exports.ClubPowerCreateRoomLayer = ClubPowerCreateRoomLayer;
})(window);



